package com.explore.demo.utils;

import com.explore.demo.VO.MailInfoVO;
import com.explore.demo.common.ServerResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.MailMessage;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMailMessage;
import org.springframework.mail.javamail.MimeMessagePreparator;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.mail.Session;
import javax.mail.internet.MimeMessage;
import java.net.Authenticator;
import java.util.Properties;

/**
 * @author gaolainan
 */
@Component
public class MailUtils
{
    @Value("${spring.mail.username}")
    private String mailSender;

    @Resource
    private JavaMailSender javaMailSender;

    private Logger logger = LoggerFactory.getLogger(MailUtils.class);

    /**
     * 发送文本邮件
     * @param mailInfoVO 封装邮件实体类
     */
    public boolean sendSimpleMail(MailInfoVO mailInfoVO) {
        try {
            SimpleMailMessage mailMessage= new SimpleMailMessage();
            mailMessage.setFrom(mailSender);
            mailMessage.setTo(mailInfoVO.getRecipient());
            mailMessage.setSubject(mailInfoVO.getSubject());
            mailMessage.setText(mailInfoVO.getContent());

            javaMailSender.send(mailMessage);
        } catch (Exception e) {
            logger.error("邮件发送失败："+e.getMessage());
            return false;
        }
        logger.error("邮件发送成功 ===> 接收人："+mailInfoVO.getRecipient());
        return true;
    }

    public boolean sendFileMail(MailInfoVO mailInfoVO, MultipartFile file){

        return true;
    }
}
